import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_module/app/app_page.dart';
import 'package:flutter_module/channels/tabbar_channel.dart';
import 'package:flutter_module/global.dart';
import 'package:flutter_module/pages/detail_page.dart';
import 'package:flutter_module/utils/navigator_util.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MineApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
          primaryColor: Colors.blue
      ),
      home: MinePage(),
      navigatorObservers: [routeObserver],
    );
  }
}

class MinePage extends StatefulWidget {
  @override
  _MinePageState createState() => _MinePageState();
}

// 2. 混入 RouteAware
class _MinePageState extends State<MinePage> with RouteAware {

  @override
  void initState() {
    super.initState();
    setupApp();
  }

  void setupApp() async {
    // 设置持久化数据
    // 实例化
    preferences = await SharedPreferences.getInstance();

    // 设置string类型
    await preferences?.setString("data", "我是数据存储的测试数据啊");

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("我的页面 - Flutter页面"),
      ),
      body: Container(
        color: Colors.blue,
        width: double.infinity,
        height: double.infinity,
        child: Center(
          child: InkWell(
            child: Text("试数据测试测试数据测试"),
            onTap: () {
              NavigatorUtil.pushPage(context, DetailPage(), pageName: "login");
            },
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton.extended(
        label: Text(
          "跳转详情页",
          style: TextStyle(
              color: Colors.black
          ),
        ),
        backgroundColor: Colors.white,
        onPressed: () {
          // NavigatorUtil.present(context, DetailPage(), "detail");
          NavigatorUtil.pushPage(context, DetailPage());
        },
      ),
    );
  }

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 3. 订阅路由监听器
    routeObserver.subscribe(this, ModalRoute.of(context) as PageRoute<dynamic>);
  }

  @override
  void dispose() {
    // 4. 取消订阅路由监听器
    routeObserver.unsubscribe(this);
    super.dispose();
  }

  void didPopNext() {
    // 5. 返回到当前页面
    TabBarController.showTab();
  }

  void didPushNext() {
    // 6. 跳转到下一个页面
    TabBarController.hideTab();
  }

}